package com.suixun.controller;


import com.github.pagehelper.PageInfo;
import com.suixun.controller.base.BaseController;
import com.suixun.controller.vo.NewsCenterVo;
import com.suixun.domain.NewsCenter;
import com.suixun.service.NewsCenterService;
import com.suixun.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 新闻中心 前端控制器
 * </p>
 *
 * @author jacky
 * @since 2018-01-11
 */
@Controller
@RequestMapping("/newsCenter")
public class NewsCenterController extends BaseController {

    @Resource
    private NewsCenterService newsCenterService;

    @GetMapping("/{type}/{page}/{size}")
    public String index(@PathVariable Integer type, @PathVariable Integer page, @PathVariable Integer size, Model model) {
        PageInfo<NewsCenter> newsCenterPageInfo = newsCenterService.pageNewsCenterExclusionContent(page, size, null, type, 1);
        if (newsCenterPageInfo != null && !CollectionUtils.isEmpty(newsCenterPageInfo.getList())) {
            List<NewsCenterVo> newsCenterVos = new ArrayList<>();
            for (NewsCenter newsCenter : newsCenterPageInfo.getList()) {
                NewsCenterVo newsCenterVo = new NewsCenterVo();
                BeanUtils.copyProperties(newsCenter, newsCenterVo);
                newsCenterVo.setCreateMonth(DateUtil.getEnglishMonth(newsCenterVo.getCreateTime()));
                newsCenterVo.setCreateDate(DateUtil.getDate(newsCenterVo.getCreateTime()));
                newsCenterVos.add(newsCenterVo);
            }
            PageInfo<NewsCenterVo> pageInfo = new PageInfo<>();
            BeanUtils.copyProperties(newsCenterPageInfo, pageInfo);
            pageInfo.setList(newsCenterVos);
            model.addAttribute("newsCenterPage", pageInfo);
        }
        model.addAttribute("hotNews", newsCenterService.listHotNewCenter(null));
        return "news";
    }

    @GetMapping("/hotNews")
    @ResponseBody
    public Object getHotNews(String title) {
        return success(newsCenterService.listHotNewCenter(title));
    }

    @GetMapping("/getNews/{id}")
    public String getNewsCenter(@PathVariable Integer id, Model model) throws UnsupportedEncodingException {
        // 修改阅读量
        newsCenterService.updateReadingNum(id);
        // 获取数据
        NewsCenter newsCenter = newsCenterService.getById(id);
        if (newsCenter != null) {
            NewsCenterVo newsCenterVo = new NewsCenterVo();
            BeanUtils.copyProperties(newsCenter, newsCenterVo);
            newsCenterVo.setCreateMonth(DateUtil.getEnglishMonth(newsCenterVo.getCreateTime()));
            newsCenterVo.setCreateDate(DateUtil.getDate(newsCenterVo.getCreateTime()));
            if (newsCenterVo.getContent() != null) {
                newsCenterVo.setContent(URLDecoder.decode(newsCenterVo.getContent(), "UTF-8"));
            }
            model.addAttribute("newsCenter", newsCenterVo);
        }
        model.addAttribute("hotNews", newsCenterService.listHotNewCenter(null));
        return "single";
    }
}
